package com.ybkj.o2o.mc.manager.impl;

import com.ybkj.o2o.mc.common.DataGrid;
import com.ybkj.o2o.mc.entity.PassengerGrade;
import com.ybkj.o2o.mc.entity.PassengerGradeAndArea;
import com.ybkj.o2o.mc.manager.PassengerGradeManager;
import com.ybkj.o2o.mc.service.EnterpriseService;
import com.ybkj.o2o.mc.service.PassengerGradeAndAreaService;
import com.ybkj.o2o.mc.service.PassengerGradeService;

import com.ybkj.o2o.mc.service.PassengerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by shine on 15/8/19.
 */
@Service
public class DefaultPassengerGradeManager implements PassengerGradeManager {

  @Autowired
  private PassengerGradeService passengerGradeService;

  @Autowired
  private PassengerGradeAndAreaService passengerGradeAndAreaService;

  @Autowired
  private PassengerService passengerService;

  @Autowired
  private EnterpriseService enterpriseService;

  @Override
  public DataGrid<PassengerGrade> findAll(int page, int rows) {

    int offset = (page - 1) * rows;

    List<PassengerGrade> list = passengerGradeService.findAll(offset, rows);

    long count = passengerGradeService.count();
    DataGrid<PassengerGrade> dataGrid = new DataGrid<>();
    dataGrid.setRows(list);
    dataGrid.setTotal(count);
    return dataGrid;
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public PassengerGrade insert(PassengerGrade passengerGrade) {

    passengerGrade.setCreated(new Date());
    passengerGrade.setUpdated(new Date());
    passengerGrade.setVersion(1);
    passengerGradeService.insert(passengerGrade);

    return passengerGrade;
  }

  @Override
  public boolean exists(String name, String appKey) {

    PassengerGrade grade = passengerGradeService.findByNameAndAppKey(name, appKey);

    return grade != null;
  }


  @Override
  public PassengerGrade fetch(Long gradeId, Long companyId, String appKey) {

    PassengerGrade fetch = passengerGradeService.fetch(gradeId, companyId, appKey);
    if (fetch != null) {
      List<PassengerGradeAndArea> pa = passengerGradeAndAreaService.findByIdAndAppKey(fetch.getId(), fetch.getAppKey());
      fetch.setPassengerGradeAndAreas(pa);
    }
    return fetch;
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public PassengerGrade updateByPrimaryKey(PassengerGrade passengerGrade) {

    passengerGradeService.update(passengerGrade);

    return passengerGrade;
  }


  @Override
  @Transactional(rollbackFor = Exception.class)
  public int deleteGrade(Long gradeId, String appKey) {

      passengerService.batchUpdateGrade(gradeId,appKey);

      enterpriseService.batchUpdateGrade(gradeId,appKey);

      passengerGradeAndAreaService.delete(gradeId,appKey);

      return passengerGradeService.delete(gradeId, appKey);

  }

  @Override
  public List<PassengerGrade> findByAppKey(String appKey) {

    return passengerGradeService.findByAppKey(appKey);
  }

  @Override
  public PassengerGrade findDefault(Long companyId,String appKey) {

    return passengerGradeService.findDefault(companyId,appKey);
  }

  @Override
  public PassengerGrade findDefaultByCompanyId(Long companyId, String appKey) {
    
    return passengerGradeService.findDefaultByCompanyId(companyId,appKey);
  }

  @Override
  public int updateDefaultGrade(boolean defaultGrade, Long id, Long companyId, String appKey) {
    
    return passengerGradeService.updateDefaultGrade(defaultGrade,id,companyId,appKey);
  }

  @Override
  public PassengerGrade findByNameAndCompanyId(String name, Long companyId, String appKey) {
    
    return passengerGradeService.findByNameAndCompanyId(name,companyId,appKey);
  }

  @Override
  public Long countByNameAndAppKey(String name, Long companyId, String appKey) {
    
    return passengerGradeService.countByNameAndAppKey(name,companyId, appKey);
  }

  @Override
  public List<PassengerGrade> findByCompany(Long companyId, String appKey) {
    
    return passengerGradeService.findByCompany(companyId,appKey);
  }

  @Override
  public int updateCompanyName(Long companyId,String abbreviation, String appKey) {
    
    return passengerGradeService.updateCompanyName(companyId,abbreviation,appKey);
  }


  @Override
  public PassengerGrade findByIdAndAppKey(Long gradeId, String appKey) {

    return passengerGradeService.findByIdAndAppKey(gradeId, appKey);
  }

}
